﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     qstringtools.cpp
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-09 00:45
///////////////////////////////////////////////////////////////////////////
#include "qstringtools.h"

QStringTools::QStringTools()
{

}

QString QStringTools::secondToString(uint64_t m_second)
{
    QString timeStr;
    uint64_t day= floor(m_second / 86400);
    uint64_t hour = floor((m_second % 86400) / 3600);
    uint64_t minute = floor(m_second % 86400 % 3600 / 60);
    uint64_t second =floor(m_second % 86400 % 3600 % 60);

    if(day > 0){
        timeStr = QString("%1天%2时%3分").arg(day).arg(hour).arg(minute);
    }else{
        timeStr = QString("%1时%2分%3秒").arg(hour).arg(minute).arg(second);
    }

    return timeStr;
}

QString QStringTools::byteSizeToString(uint64_t size)
{
    QString strUnit;
    double dSize = size * 1.0;
    if (dSize <= 0)
    {
        dSize = 0.0;
    }
    else if (dSize < 1024)
    {
        strUnit = "Bytes";
    }
    else if (dSize < 1024 * 1024)
    {
        dSize /= 1024;
        strUnit = "KB";
    }
    else if (dSize < 1024 * 1024 * 1024)
    {
        dSize /= (1024 * 1024);
        strUnit = "MB";
    }
    else if (dSize < 1024 * 1024 * 1024 * 1024.0)
    {
        dSize /= (1024 * 1024 * 1024);
        strUnit = "GB";
    }
    else
    {
        dSize /= (1024 * 1024 * 1024 * 1024.0);
        strUnit = "TB";
    }
    return QString("%1 %2").arg(QString::number(dSize, 'f', 2)).arg(strUnit);
}

QString QStringTools::numberDivision(double value, int interval,int precision)
{
    QString strVal = QString::number(value, 'f', precision);
    QString outStr = "";
    int num = 1;
    int startPos = strVal.length()-1;
    if(strVal.lastIndexOf('.') != -1){
        outStr = strVal.right(strVal.length()-strVal.lastIndexOf('.'));
        startPos = strVal.lastIndexOf('.')-1;
    }
    for(int pos = startPos;pos >= 0;pos--){
        outStr.insert(0,strVal.at(pos));
        if(num % interval == 0 & pos !=0){
            outStr.insert(0,',');
        }
        num ++;
    }
    return outStr;
}
